<template>
    <div class="register-container">
      <h2>注册</h2>
      <form @submit.prevent="register">
        <!-- 注册需要收集的信息可能与登录不同 -->
        <div class="form-control">
          <label for="username">用户名:</label>
          <input type="text" id="" v-model="username" required>
        </div>
        <div class="form-control">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="password" required>
        </div>
        <!-- <div class="form-control">
          <label for="email">邮箱:</label>
          <input type="email" id="email" v-model="email" required>
        </div> -->
        <div class="form-control">
          <label for="tel">手机号:</label>
          <input type="tel" id="tel" v-model="tel" required>
        </div>
        <button type="submit" class="register-btn">注册</button>
        <router-link to="/login" class="login-link">已有账号？去登录</router-link>
      </form>
      <p v-if="error" class="error-message">{{ error }}</p>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  axios.defaults.baseURL="http://localhost:8087"
  export default {
    data() {
      return {
        username: '',
        password: '',
        tel:'',
        error: ''
      };
    },
    methods: {
      register() {
        // 注册逻辑，可能包含验证和发送请求到后端
        axios.get(`/findUserByName/${this.username}`).then(res=>{
          if(res.data)
          {
            alert('用户名重复，注册失败')
          }
          else{
            let userid=null
            axios.get(`/findUser_total`).then(res=>{
              userid=res.data.total+1
              axios.post(`/insertuser?userid=`+userid+`&username=`+this.username+`&password=`+this.password+`&phone=`+this.tel).then(res=>{
                console.log(res.data)
                alert('注册成功');
                this.username='';
                this.password= '',
                this.tel='',
                this.error= ''
              })
              
            })
            
          }
        })
        
      }
    }
  }
  </script>
  
  <style scoped>
.register-container {
  width: 100%;
  max-width: 400px;
  margin: 30px auto;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: #333;
}

.form-control {
  margin-bottom: 15px;
  position: relative;
}

.form-control label {
  display: block;
  margin-bottom: 5px;
}

.form-control input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.register-btn {
  display: block;
  width: 100%;
  padding: 10px;
  border: none;
  background-color: #007bff;
  color: white;
  border-radius: 4px;
  cursor: pointer;
}

.register-btn:hover {
  background-color: #0056b3;
}

.error-message {
  color: red;
  text-align: center;
  margin-top: 5px;
}

/* 响应式设计 */
@media (max-width: 600px) {
  .register-container {
    padding: 15px;
  }
}
</style>